package com.qiqi.admin.order.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.base.BaseModel;
import com.qiqi.common.entity.PageEntity;
import com.qiqi.endproductwarehouse.entity.EndProductWarehouseDO;
import com.qiqi.endproductwarehouse.model.EndProductWarehouseDTO;
import com.qiqi.endproductwarehouse.service.EndProductWarehouseService;
import com.qiqi.order.dto.InwarehouseDTO;
import com.qiqi.order.entity.InwarehouseDO;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.order.service.InwarehouseService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-12-08
 */
@Api("相关接口")
@RestController
@RequestMapping("/inwarehouse")
public class InwarehouseController {

    @Resource
    private InwarehouseService inwarehouseService;

    protected SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");

    @Resource
    private EndProductWarehouseService endProductWarehouseService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("/list")
    public PageEntity<EndProductWarehouseDTO> getInwarehousePage(@RequestBody InwarehouseDTO inwarehouse) {
        IPage<EndProductWarehouseDTO> iPage = inwarehouseService.getList(new Page<InwarehouseDTO>(inwarehouse.getPage(),inwarehouse.getCount()),inwarehouse.getNo(),inwarehouse.getName());
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<EndProductWarehouseDTO>>() {}, iPage.getRecords()));
    }

    @PostMapping("/inwarehouse")
    public boolean inWarehouse(@RequestBody EndProductWarehouseDTO productWarehouse){
        EndProductWarehouseDO endProductWarehouseDO = new EndProductWarehouseDO();
        List<EndProductWarehouseDO> endProductWarehouse = endProductWarehouseService.list(new QueryWrapper<EndProductWarehouseDO>().eq("order_id", productWarehouse.getOrderId()));
        if (endProductWarehouse.size() !=0){
            Integer productNum = endProductWarehouse.get(0).getProductNum();
            String endProduct = endProductWarehouse.get(0).getEndProductPos();
            Integer integer = Integer.parseInt(endProduct);
            //成品仓库
            endProductWarehouseDO.setProductNum(productWarehouse.getPNum()+productNum);
            endProductWarehouseDO.setStorageQuantity(productWarehouse.getPNum()+integer);
            endProductWarehouseDO.setEndProductPos(String.valueOf(productWarehouse.getPNum()+integer));
            endProductWarehouseDO.setOrderId(productWarehouse.getOrderId().toString());
            endProductWarehouseDO.setWarehouseNo(dateFormat.format(new Date()));
        }else{
            //成品仓库
            endProductWarehouseDO.setProductNum(productWarehouse.getPNum());
            endProductWarehouseDO.setEndProductPos(String.valueOf(productWarehouse.getPNum()));
            endProductWarehouseDO.setOrderId(productWarehouse.getOrderId().toString());
            endProductWarehouseDO.setWarehouseNo(dateFormat.format(new Date()));
            endProductWarehouseDO.setStorageQuantity(productWarehouse.getPNum());
        }
        endProductWarehouseService.saveOrUpdate(endProductWarehouseDO,new QueryWrapper<EndProductWarehouseDO>().eq("order_id",productWarehouse.getOrderId()));
        InwarehouseDO inwarehouse = new InwarehouseDO();
        inwarehouse.setId(productWarehouse.getEId());
        inwarehouse.setState(true);
        return inwarehouseService.updateById(inwarehouse);
    }

    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public InwarehouseDO getInwarehouse(@PathVariable Long id) {
        return inwarehouseService.getById(id);
    }

}
